<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
        }

        .table .item {
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
            float: left;
        }
    </style>
</head>

<body>
    <div id="app">
        <my-add @addlistitem="addListItem"></my-add>
        <my-table :list="list" @delUserById="delUserById"></my-table>
    </div>
</body>
<template id="mytable">
    <div class="table">
        <div class="tr">
            <div class="item">编号</div>
            <div class="item">用户名</div>
            <div class="item">密码</div>
            <div class="item">操作</div>
        </div>
        <div class="tr" v-for="item in list">
            <div class="item">{{item.id}}</div>
            <div class="item">{{item.username}}</div>
            <div class="item">{{item.userpwd}}</div>
            <div class="item" @click="del(item.id)">删除</div>
        </div>


    </div>
</template>
<template id="myadd">
    <div>
        <div>
            用户名<input v-model="username" />
        </div>
        <div>
            密码<input v-model="userpwd" />
        </div>
        <button @click="add">新增</button>
    </div>
</template>
<script>
    let myAdd = {
        data() {
            return {
                username: "",
                userpwd: ""
            }
        },
        methods: {
            add() {
                this.$emit("addlistitem", {
                    username: this.username,
                    userpwd: this.userpwd
                })
            }
        },
        template: "#myadd"
    }
    let myTable = {
        props: ["list"],
        template: "#mytable",
        methods: {
            del(id) {
                this.$emit("deluserbyid", id);
            }
        }
    }
    new Vue({
        el: "#app",
        data: {
            list: [],
        },
        methods: {
            async addListItem(item) {//item是一个对象  
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfos",
                    params: {
                        username: item.username
                    }
                })
                if (list.length) {
                    alert("该用户名已经存在");
                    return false;
                }

                let { data } = await axios({
                    method: "post",
                    url: "http://localhost:2345/userinfos",
                    data: item
                })
                this.list.push(data);


            },
            async delUserById(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2345/userinfos/" + id,
                })
                this.list = this.list.filter(item => item.id != id);

            }
        },
        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2345/userinfos",
                params: {}
            })
            this.list = list;//请求数据
        },
        components: {
            myTable,
            myAdd
        }
    })

</script>

</html>